package com.vcms.common.util.impl;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.vcms.common.util.PageBean;


@Component
@Scope("prototype")
public class PageBeanImpl implements PageBean {
	private long totalRow; // 进行分页所有记录的总行数
	private int pageSize = 20; // 每页显示的总行数
	private int currentPage = 1; // 当前页号，默认为1
	private int totalPage; // 分页后的总页数

	public PageBeanImpl() {
	}

	public int getTotalPage() {
		return totalPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setTotalRow(long totalRows) {
		this.totalRow = totalRows;
		// 重新计算总页数
		calcTotalPage();
	}

	public void setTotalPage(int totalPages) {
		this.totalPage = totalPages;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
		// 重新计算总页数
		calcTotalPage();
	}

	public long getTotalRow() {
		return totalRow;
	}

	/*
	 * 计算总页数
	 */
	private void calcTotalPage() {
		totalPage = (int) totalRow / pageSize;
		int mod = (int) totalRow % pageSize;
		if (mod > 0) {
			totalPage++; // 最后一页记录数不够，总页数加上一页
		}

		// 限制当前页值在1到总页数之间
		if (currentPage > totalPage) {
			this.currentPage = totalPage;
		}
		if (currentPage <= 0) {
			this.currentPage = 1;
		}
	}

	// 下一页的第一条记录
	public int getNextResult() {
		return this.getPageSize() * (this.getCurrentPage() - 1);
	}

}
